package com.thtn.greendroidthtneg2;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class WordListActivity extends GDActivity {
	private static final String TAG = "WordListActivity";
	final byte WEB_DIALOG = 0;
	long id = -1L;
	private EditText mEditText;
	private ListView mListView;
	String value;
	View view;

	private int showResults(String paramString) {
		Log.d(TAG, "Calling showResults ..." + paramString);
		return getContentResolver().query(DictionaryProvider.CONTENT_URI, null,
				null, new String[] { paramString }, null).getCount();

	}

	protected Dialog onCreateDialog(int paramInt) {
		switch (paramInt) {
		default:
			return null;
		case 0:
			return null;
		case 1:
			ProgressDialog localProgressDialog = new ProgressDialog(this);
			localProgressDialog.setMessage(getString(R.string.loading));
			return localProgressDialog;
		}

	}

	private void showList(final String paramString) {
		Log.d(TAG, "Calling showList ...");
		this.mListView = ((ListView) findViewById(R.id.list));
		this.mEditText = ((EditText) findViewById(R.id.editText));
		this.view = findViewById(R.id.view);
		String[] arrayOfString = new String[] { "suggest_text_1" };
		int[] arrayOfInt = new int[] { R.id.word };
		Cursor localCursor = managedQuery(DictionaryProvider.CONTENT_URI, null,
				null, new String[] { paramString }, null);
		Log.d(TAG, "localCursor finished");

		// final MyCursorAdapter localMyCursorAdapter = new
		// MyCursorAdapter(this,
		// R.layout.result, localCursor, arrayOfString, arrayOfInt);

		// localCursor.setNotificationUri(getContentResolver(),
		// DictionaryProvider.CONTENT_URI);

	}

	//
	// public void beforeTextChanged(
	// CharSequence paramAnonymousCharSequence,
	// int paramAnonymousInt1, int paramAnonymousInt2,
	// int paramAnonymousInt3) {
	// }
	//
	// public void onTextChanged(CharSequence paramAnonymousCharSequence,
	// int paramAnonymousInt1, int paramAnonymousInt2,
	// int paramAnonymousInt3) {
	// }
	// });
	// this.mListView
	// .setOnItemClickListener(new AdapterView.OnItemClickListener() {
	// public void onItemClick(
	// AdapterView<?> paramAnonymousAdapterView,
	// View paramAnonymousView, int paramAnonymousInt,
	// long paramAnonymousLong) {
	// Intent localIntent = new Intent(WordListActivity.this
	// .getBaseContext(), WordActivity.class);
	// localIntent.putExtra("Selection",
	// WordListActivity.this.value + "<sep>"
	// + paramAnonymousLong);
	// WordListActivity.this.startActivity(localIntent);
	// }
	// });
	// this.mListView
	// .setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
	// public boolean onItemLongClick(
	// AdapterView<?> paramAnonymousAdapterView,
	// View paramAnonymousView, int paramAnonymousInt,
	// long paramAnonymousLong) {
	// WordListActivity.this.id = WordListActivity.this.mListView
	// .getItemIdAtPosition(paramAnonymousInt);
	// WordListActivity.this.showDialog(0);
	// return true;
	// }
	// });
	//
	// }
	//
	class MyCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
		AlphabetIndexer alphaIndexer;

		public MyCursorAdapter(Context context, int layout, Cursor c,
				String[] from, int[] to) {
			super(context, layout, c, from, to);
			Log.d(TAG, "MyCursorAdapter after super ...");
			alphaIndexer = new AlphabetIndexer(c,
					c.getColumnIndexOrThrow("suggest_text_1"),
					"#ABCDEFGHIJKLMNOPQRSTUVWXYZ");
			Log.d(TAG, "MyCursorAdapter after super ...");

			alphaIndexer.setCursor(c);
			Log.d(TAG, "MyCursorAdapter init ...");

		}

		public void bindView(View paramView, Context paramContext,
				Cursor paramCursor) {
			Log.d(TAG, "MyCursorAdapter bindView ...");
			WordListActivity.ViewHolder localViewHolder = (WordListActivity.ViewHolder) paramView
					.getTag();
			int i = paramCursor.getPosition();
			int j = getSectionForPosition(i);
			if (getPositionForSection(j) == i) {
				localViewHolder.header.setVisibility(0);
				String str = this.alphaIndexer.getSections()[j].toString()
						.trim();
				localViewHolder.header.setText(str);
			}

			localViewHolder.textView.setText(paramCursor.getString(paramCursor
					.getColumnIndex("suggest_text_1")));
			localViewHolder.header.setVisibility(8);

		}

		public int getPositionForSection(int paramInt) {
			Log.d(TAG, "MyCursorAdapter getPositionForSection ...");
			return this.alphaIndexer.getPositionForSection(paramInt);
		}

		public int getSectionForPosition(int paramInt) {
			return this.alphaIndexer.getSectionForPosition(paramInt);
		}

		public Object[] getSections() {
			return this.alphaIndexer.getSections();
		}

		public View newView(Context paramContext, Cursor paramCursor,
				ViewGroup paramViewGroup) {
			View localView = WordListActivity.this.getLayoutInflater().inflate(
					R.layout.section_header_list, paramViewGroup, false);
			WordListActivity.ViewHolder localViewHolder = new WordListActivity.ViewHolder();
			localViewHolder.header = ((TextView) localView
					.findViewById(R.id.listHeader));
			localViewHolder.textView = ((TextView) localView
					.findViewById(R.id.listText));
			localView.setTag(localViewHolder);
			return localView;
		}

	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setActionBarContentView(R.layout.wordlist_layout);
		getActionBar().setType(ActionBar.Type.Normal);
		addActionBarItem(ActionBarItem.Type.SortBySize);
		Bundle localBundle = getIntent().getExtras();
		if (localBundle != null) {
			this.value = localBundle.getString("Selection");
			setTitle(this.value);

			if (this.value != null) {
				BackgroundThreadListClick localBackgroundThreadListClick = new BackgroundThreadListClick();
				String[] arrayOfString1 = new String[1];
				arrayOfString1[0] = this.value;
				localBackgroundThreadListClick.execute(arrayOfString1);
			}

		} else {
			setTitle("No found!");
		}
	}

	class BackgroundThreadListClick extends AsyncTask<String, String, String> {
		BackgroundThreadListClick() {
		}

		protected String doInBackground(String[] paramArrayOfString) {
			try {
				if (WordListActivity.this.showResults(paramArrayOfString[0]) == 0) {

					String[] arrayOfString = new String[1];
					arrayOfString[0] = paramArrayOfString[0];
					publishProgress(arrayOfString);
					WordListActivity.this.finish();

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			Log.d(TAG, "doInBackground finished!" + paramArrayOfString);
			return paramArrayOfString[0];
		}

		protected void onPostExecute(String paramString) {
			Log.d(TAG, "onPostExecute -> showList ..." + paramString);
			WordListActivity.this.showList(paramString);
			// WordListActivity.this.dismissDialog(1);
		}

		protected void onPreExecute() {
			// WordListActivity.this.showDialog(1);
		}

		protected void onProgressUpdate(String[] paramArrayOfString) {
			Toast.makeText(
					WordListActivity.this.getBaseContext(),
					WordListActivity.this.getString(R.string.no_my_words) + " "
							+ paramArrayOfString[0] + " word list", 1).show();
		}
	}

	public static class ViewHolder {
		public TextView header;
		public TextView textView;
	}
}
